//
//  XDScrollView.m
//  XDEducation
//
//  Created by apple on 2017/4/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDScrollView.h"

@interface XDScrollView ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, strong) UIScrollView *scrollView;
/**
 *  定时器
 */
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation XDScrollView
{
    NSInteger _imageCount;    //图片数量
}


-(instancetype)initWithFrame:(CGRect)frame imageArr:(NSMutableArray *)imageArr{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        
        _imageCount = imageArr.count;
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.height - 50, frame.size.width, 50)];
        
        [self addSubview:self.scrollView];
        
        [self addSubview:self.pageControl];
        
        // 0.尺寸参数
        CGFloat imageW = self.scrollView.frame.size.width;
        CGFloat imageH = self.scrollView.frame.size.height;
        CGFloat imageY = 0;
        
        // 1.添加张图片到scrollView中
        for (int i = 0; i < imageArr.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.tag = i;
            // 设置frame
            CGFloat imageX = i * imageW;
            imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
            
            // 设置图片
            NSString *name = imageArr[i];
            //imageView.image = [UIImage imageNamed:name];
            [imageView sd_setImageWithURL:[NSURL URLWithString:name] placeholderImage:nil options:0];
            
            [self.scrollView addSubview:imageView];
            
            
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageClicked:)];
            
            [imageView addGestureRecognizer:tap];
            
        }
        
        // 2.设置内容尺寸
        CGFloat contentW = imageArr.count * imageW;
        self.scrollView.contentSize = CGSizeMake(contentW, 0);
        self.scrollView.delegate = self;
        
        // 3.隐藏水平滚动条
        self.scrollView.showsHorizontalScrollIndicator = NO;
        //self.pageControl.numberOfPages = 5;
        //self.pageControl.currentPage = 0;
        self.pageControl.enabled = NO;
        // 4.分页
        self.scrollView.pagingEnabled = YES;
        
        // 5.设置pageControl的总页数
        self.pageControl.numberOfPages = imageArr.count;
        
        // 6.添加定时器(每隔2秒调用一次self 的nextImage方法)
        [self addTimer];

        
    }
    return self;

}


/**
 *  添加定时器
 */
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

/**
 *  移除定时器
 */
- (void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)nextImage
{
    
    
    
    // 1.增加pageControl的页码
    long page = 0;
    if (self.pageControl.currentPage == _imageCount - 1) {
        page = 0;
    } else {
        page = self.pageControl.currentPage + 1;
    }
    
    if (self.imageChangedBlock) {
        
        self.imageChangedBlock(page);
    }
    
    // 2.计算scrollView滚动的位置
    CGFloat offsetX = page * self.scrollView.frame.size.width;
    CGPoint offset = CGPointMake(offsetX, 0);
    [self.scrollView setContentOffset:offset animated:YES];
    
    
    
}

#pragma mark - scrollView代理方法
/**
 *  当scrollView正在滚动就会调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 根据scrollView的滚动位置决定pageControl显示第几页
    CGFloat scrollW = scrollView.frame.size.width;
    int page = (scrollView.contentOffset.x + scrollW * 0.5) / scrollW;
    self.pageControl.currentPage = page;
}

/**
 *  开始拖拽的时候调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 停止定时器(一旦定时器停止了,就不能再使用)
    [self removeTimer];
}

/**
 *  停止拖拽的时候调用
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
    //    long page = 0;
    //    if (self.pageControl.currentPage == _imageCount - 1) {
    //        page = 0;
    //    } else {
    //        page = self.pageControl.currentPage + 1;
    //    }
    //
    //    if (self.imageChangedBlock) {
    //
    //        self.imageChangedBlock(page);
    //    }
    //
    //开启定时器
    [self addTimer];
}

-(void)imageClicked:(UITapGestureRecognizer *)tap{
    
    //    NSLog(@"点击第%ld张图片",tap.view.tag);
    
    if (self.imageClickedBlock) {
        self.imageClickedBlock(tap.view.tag);
    }
    
}

@end
